using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Biology_Shark_1_Ctrl : Unit_Biology_Ctrl
{
    public float moveSpeed = 2;
    public float rotateSpeed = 1;
    public Transform rootBone;
    public Transform headBone;
    public List<Transform> tailBones;

    public Transform mouthUpBone;
    public Transform mouthDownBone;

    public Transform target;

    public float currentHeadAngle=0;
    public float headAngleVelocity;
    public Vector3 bodyAngleVelocity;
    // Start is called before the first frame update
    public override void Start()
    {
        Vector3 targetDirection = target.transform.position - (headBone.transform.position + headBone.right);
        currentHeadAngle = Vector3.SignedAngle(headBone.parent.right, targetDirection, Vector3.forward);
    }

    // Update is called once per frame
    public override void Update()
    {
        int flip = target.transform.position.x < this.transform.position.x ? -1 : 1;
        //rootBone.transform.localScale = new Vector3(1, flip, 1);
        Vector3 targetDirection = target.transform.position - (headBone.transform.position);
        Debug.DrawLine(headBone.transform.position, (headBone.transform.position + targetDirection.normalized));

        float targetHeadAngle = Vector3.SignedAngle(headBone.parent.right, targetDirection, Vector3.forward);
        if (Mathf.Abs(targetHeadAngle) > 120)
        {
            targetHeadAngle = -120;
        }
        currentHeadAngle = Mathf.SmoothDamp(currentHeadAngle, targetHeadAngle,ref headAngleVelocity, 0.16f);//Time.deltaTime * Mathf.Lerp(0, 240, Mathf.Abs(targetHeadAngle - currentHeadAngle) / 30f)

        Debug.DrawLine(headBone.transform.position, (headBone.transform.position + headBone.right));

        headBone.localEulerAngles = new Vector3(0, 0, Mathf.Clamp(currentHeadAngle, -60, 60));
        for (int i = 0; i < tailBones.Count; i++)
        {
            tailBones[i].localEulerAngles = new Vector3(0, 0, -Mathf.Clamp(currentHeadAngle, -100, 100) * (1f / (tailBones.Count - 1)));
        }
        mouthUpBone.transform.localEulerAngles = new Vector3(0, 0, Mathf.Lerp(40, -5, ((targetDirection).magnitude) / 3));
        mouthDownBone.transform.localEulerAngles = new Vector3(0, 0, Mathf.Lerp(-50, -5, ((target.transform.position - (targetDirection)).magnitude) / 3));

        //renderer.flipY = target.transform.position.x < this.transform.position.x ? true : false;
        this.transform.position += headBone.right * Time.deltaTime * moveSpeed * Mathf.Lerp(1, 0.1f, Mathf.Abs(currentHeadAngle) / 60);
        this.transform.right = Vector3.SmoothDamp(this.transform.right, headBone.transform.right, ref bodyAngleVelocity, Mathf.Lerp(0.16f, 0.32f, Mathf.Abs(currentHeadAngle) / 120)); //new Vector3(0, 0, Mathf.MoveTowards(this.transform.InspectorEulers().z, targetHeadAngle, Time.deltaTime * rotateSpeed * Mathf.Lerp(30, 240, Mathf.Abs(currentHeadAngle) / 120)));
        this.transform.eulerAngles = new Vector3(0, 0, this.transform.eulerAngles.z);
    }
}
